<?php
namespace jy_ad;
defined('BY_JYA') or exit('error');
/*
	能量值
*/
class nfee extends \plugin{
	/*
		downs
	*/
	public function add_fee($order=array()){
		if($this->fenxiaoset['nfee_rate']<=0){
			return false;
		}
		//现在支付方式分佣
		$nfee_paytype=$this->fenxiaoset['nfee_paytype'];
		if($nfee_paytype){
			$paylog=pdo_get('core_paylog',array('id'=>$order['pay_id']),array('pay_type','di_credit','di_fee','fee'));
			if($paylog['di_fee']>0){
				if(strpos($nfee_paytype,';'.$paylog['di_credit'].';') !== false){
					$order['price'] -= $paylog['di_fee'];
				}
				if(strpos($nfee_paytype,';'.$order['paytype'].';') !== false){
					$order['price'] -= $paylog['fee']-$paylog['di_fee'];
				}
				//两种支付都不支持
				if(strpos($nfee_paytype,';'.$paylog['di_credit'].';') !== false && strpos($nfee_paytype,';'.$order['paytype'].';') !== false){
					return;
				}
			}elseif(strpos($nfee_paytype,';'.$order['paytype'].';') !== false){
				return;
			}
		}
		if($teamsetting['nfee_pricetype']!=2){
			$commission_price=$order['price'];
		}else{
			$commission_price=$order['price']-$order['dispatchprice'];
		}
		if($commission_price<=0){
			return;
		}
		$nfee=$commission_price*$this->fenxiaoset['nfee_rate']/100;
		$nfee=$this->check_canfee($nfee);
		if($nfee>0){
			model('core/member')->member_credit($order['mid'],'credit3',$nfee,true,'jy_ad','下单获得能量值');
			$params=array(
				'storeid'=>$order['storeid'],
				'orderid'=>$order['id'],
				'fee'=>$nfee,
				'gift_type'=>3,
				'msg'=>'下单获得能量值！',
				'status'=>1,
				'mid'=>$order['mid'],
			);
			$this->model('model')->set_feelog($params,4);
		}
	}
	public function check_canfee($nfee=0){
		$allfee=pdo_getsum('jy_ad_feelog',array('type'=>4,'uniacid'=>$_SESSION['uniacid']),'fee');
		$lastfee=$this->fenxiaoset['nfee_sfallfee']-$allfee;
		if($lastfee<=0){
			return 0;
		}elseif($lastfee<=$nfee && $nfee>0){
			return $lastfee;
		}else{
			return $nfee;
		}
	}
	//按周期，释放
	public function up_sifang(){
		$params=array('uniacid'=>$_SESSION['uniacid']);
		$lastnfee=pdo_get('jy_ad_nfee',$params,'','id desc');
		$start_time=$lastnfee['end_time'];
		//获取周期；
		$nfee_sfday=$this->fenxiaoset['nfee_sfday'];
		if($nfee_sfday){
			$month=date('Y-m');
			$end_time=strtotime($month.'-01 00:00:00');
		}else{
			$end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
		}
		if($start_time+3600>=$end_time){
			return false;
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
		$params['createtime between']=array($start_time,$end_time);
		$ordersfee=pdo_getsum('jy_weishop_order',$params,'price');
		$params=array('uniacid'=>$_SESSION['uniacid']);
		$members=$this->getall('member',$params);
		$alluser_num=count($members);
		$nfee_set=$this->fenxiaoset['nfee_set'];
		paixu($nfee_set,'price','desc');
		foreach($nfee_set as $nv){
			if($ordersfee>=$nv['price']){
				$rate=$nv['rate'];
				$fee=$rate>0?$rate*$ordersfee/100:0;
				break;
			}
		}
		if($fee>0){
			foreach($members as $item){
				$this->up_znfee($item,$fee);
			}
		}
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'user_num'=>$alluser_num,
				'fee'=>$fee,
				'orderfee'=>$ordersfee,
				'status'=>0,
				'start_time'=>$start_time,
				'end_time'=>$end_time,
		);
		$this->insert('nfee',$data);
		return true;
	}
	public function up_znfee($member=array(),$fee=0){
		if(!$this->fenxiaoset['nfee_sfallfee']){
			return false;
		}
		$credit3=pdo_getcolumn('core_members',array('id'=>$member['mid']),'credit3');
		if($credit3<=0){
			return false;
		}
		$znfee=$fee/$this->fenxiaoset['nfee_sfallfee'];
		$znfee=$znfee*$credit3;
		$znfee=$znfee/$credit3;
		if($znfee>0){
			$this->update('member',array('znfee +='=>$znfee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']));
			$params=array(
				'fee'=>$znfee,
				'gift_type'=>3,
				'msg'=>'周期增值能量值比率',
				'status'=>1,
				'mid'=>$member['mid'],
			);
			$this->model('model')->set_feelog($params,5);
		}
		return true;
	}
}